---
--类名: ButtonGroup.lua
--创建日期: 2017-6-23
--创建人: 陈小虎
--功能描述: 
--修改内容：
--修改日期:   
--修改人:     
--修改备注: 
--

local ButtonGroup = class("ButtonGroup")

function ButtonGroup:ctor()
	self._btnRoots = {}
	self._showArrowFlags = {}
	self._showBgColorFlags = {}
end


function ButtonGroup:setClickCallback(callback)
	self._clickCallback = callback
end

function ButtonGroup:clickCallback(tag)
	if self._clickCallback then
		self._clickCallback(tag)
	end
end

function ButtonGroup:addButtonRoot(root)
	table.insert(self._btnRoots,root)
	self:resetListener(root)
	self:setButtonStatus(root,false)
end

function ButtonGroup:resetListener(root)
	root:setTouchEnabled(false)
	local selBtn = root:getChildByName("selected_btn")
	selBtn:setEnabled(true)
	selBtn:setTouchEnabled(true)
	selBtn:addClickEventListener(function(sender)
		local status = selBtn:isBright()
		if not status then return end  -- 点击的是已经选中了
		self:setButtonSelected(root)
	end)

end

function ButtonGroup:setChangeNameColor(isChange)
	self._isChangeNameColor = isChange
end

function ButtonGroup:isChangeNameColor()
	return self._isChangeNameColor
end

function ButtonGroup:setButtonStatus(root,isSelected)

	root:setBackGroundColorType(1)

	local selImg = root:getChildByName("selected_img")
	local selBtn = root:getChildByName("selected_btn")
	local downArrow = root:getChildByName("img_1")
	local upArrow = root:getChildByName("img_2")
	local nameNode = root:getChildByName("name")
	local bg = root:getChildByName("bg")
	if selImg then selImg:setVisible(isSelected)end
	if downArrow then downArrow:setVisible(false) end
	if upArrow then upArrow:setVisible(false) end

	if selBtn then
		local status = not isSelected
		selBtn:setBright(status)

		if isSelected then
			self:clickCallback(selBtn:getTag())
		end

		if self:isShowArrowFlag(selBtn:getTag()) then
			if downArrow then downArrow:setVisible(isSelected) end
			if upArrow then upArrow:setVisible(not isSelected) end
		end

		if self:isShowBgColorFlag(selBtn:getTag()) then
			if self:isShowArrowFlag(selBtn:getTag()) then
				if bg and isSelected then
					root:setOpacity(255)
					root:setBackGroundColor(bg:getColor())
				else
					root:setOpacity(0)
				end
			else
				root:setOpacity(255)
				root:setBackGroundColor(bg:getColor())
			end
		else
			root:setOpacity(0)
		end
	end

	if self:isChangeNameColor() and nameNode then
		if isSelected then
			nameNode:setTextColor(cc.c4b(255, 225, 132, 255))
		else
			nameNode:setTextColor(cc.c4b(109, 109, 109, 255))
		end
	end
end

function ButtonGroup:setButtonSelected(root)
	for i,v in ipairs(self._btnRoots) do
		if v == root then
			self:setButtonStatus(v, true)
		else
			self:setButtonStatus(v, false)
		end
	end
end

function ButtonGroup:setButtonSelectedByIndex(index)
	for i,v in ipairs(self._btnRoots) do
		if i == index then
			self:setButtonStatus(v, true)
		else
			self:setButtonStatus(v, false)
		end
	end
end

function ButtonGroup:addShowArrowFlag(flag)
	table.insert(self._showArrowFlags,flag)
end

function ButtonGroup:isShowArrowFlag(flag)
	for i,v in ipairs(self._showArrowFlags) do
		if v == flag then
			return true
		end
	end
	return false
end

function ButtonGroup:addShowBgColorFlag(flag)
	table.insert(self._showBgColorFlags,flag)
end

function ButtonGroup:isShowBgColorFlag(flag)
	for i,v in ipairs(self._showBgColorFlags) do
		if v == flag then
			return true
		end
	end
	return false
end

return ButtonGroup